const StringAlgorithms = require("../template")

function testStringOperations() {
  const stringAlgo = new StringAlgorithms()

  console.log(
    "1. 字符串分割测试:",
    (() => {
      const tests = [
        { input: "hello world", delimiter: " ", expected: ["hello", "world"] },
        { input: "a,b,c", delimiter: ",", expected: ["a", "b", "c"] },
        { input: "hello", delimiter: " ", expected: ["hello"] },
        { input: "", delimiter: ",", expected: [""] },
        { input: "a", delimiter: ",", expected: ["a"] },
        { input: "hello,world,test", delimiter: ",", expected: ["hello", "world", "test"] }
      ]
      return tests.every(test => JSON.stringify(stringAlgo.splitString(test.input, test.delimiter)) === JSON.stringify(test.expected))
    })()
  )

  console.log(
    "2. 字符串连接测试:",
    (() => {
      const tests = [
        { input: ["hello", "world"], separator: " ", expected: "hello world" },
        { input: ["a", "b", "c"], separator: ",", expected: "a,b,c" },
        { input: ["hello"], separator: " ", expected: "hello" },
        { input: [], separator: ",", expected: "" },
        { input: ["a"], separator: ",", expected: "a" }
      ]
      return tests.every(test => stringAlgo.joinString(test.input, test.separator) === test.expected)
    })()
  )

  console.log(
    "3. 字符串截取测试:",
    (() => {
      const tests = [
        { input: "hello world", start: 0, end: 5, expected: "hello" },
        { input: "hello world", start: 6, end: 11, expected: "world" },
        { input: "hello", start: 0, end: 1, expected: "h" },
        { input: "hello", start: 2, end: 4, expected: "ll" },
        { input: "", start: 0, end: 0, expected: "" }
      ]
      return tests.every(test => stringAlgo.substring(test.input, test.start, test.end) === test.expected)
    })()
  )

  console.log(
    "4. 字符串查找测试:",
    (() => {
      const tests = [
        { input: "hello world", target: "world", expected: 6 },
        { input: "hello world", target: "hello", expected: 0 },
        { input: "hello", target: "ll", expected: 2 },
        { input: "hello", target: "x", expected: -1 },
        { input: "", target: "a", expected: -1 }
      ]
      return tests.every(test => stringAlgo.findSubstring(test.input, test.target) === test.expected)
    })()
  )

  console.log(
    "5. 字符串替换所有测试:",
    (() => {
      const tests = [
        { input: "hello world", oldStr: "world", newStr: "universe", expected: "hello universe" },
        { input: "hello hello", oldStr: "hello", newStr: "hi", expected: "hi hi" },
        { input: "abc", oldStr: "d", newStr: "e", expected: "abc" },
        { input: "", oldStr: "a", newStr: "b", expected: "" },
        { input: "a", oldStr: "a", newStr: "b", expected: "b" }
      ]
      return tests.every(test => stringAlgo.replaceAll(test.input, test.oldStr, test.newStr) === test.expected)
    })()
  )

  console.log(
    "6. 字符串去空格测试:",
    (() => {
      const tests = [
        { input: "  hello world  ", expected: "hello world" },
        { input: "hello world", expected: "hello world" },
        { input: "   ", expected: "" },
        { input: "", expected: "" },
        { input: "a", expected: "a" }
      ]
      return tests.every(test => stringAlgo.trim(test.input) === test.expected)
    })()
  )

  console.log(
    "7. 字符串大小写转换测试:",
    (() => {
      const tests = [
        { input: "Hello World", toUpper: true, expected: "HELLO WORLD" },
        { input: "Hello World", toUpper: false, expected: "hello world" },
        { input: "a", toUpper: true, expected: "A" },
        { input: "A", toUpper: false, expected: "a" },
        { input: "", toUpper: true, expected: "" }
      ]
      return tests.every(test => stringAlgo.changeCase(test.input, test.toUpper) === test.expected)
    })()
  )

  console.log(
    "8. 字符串长度测试:",
    (() => {
      const tests = [
        { input: "hello", expected: 5 },
        { input: "", expected: 0 },
        { input: "a", expected: 1 },
        { input: "hello world", expected: 11 },
        { input: "你好", expected: 2 }
      ]
      return tests.every(test => stringAlgo.getStringLength(test.input) === test.expected)
    })()
  )

  console.log(
    "9. this上下文字符串操作测试:",
    (() => {
      const stringOpObj = {
        multiplier: 2,
        processString: function(str) {
          const stringAlgo = new StringAlgorithms()
          const length = stringAlgo.getStringLength(str)
          return length * this.multiplier
        }
      }
      return stringOpObj.processString("hello") === 10 // 5 * 2 = 10
    })()
  )

  console.log(
    "10. 复杂对象this绑定字符串操作测试:",
    (() => {
      const stringOpConfigObj = {
        config: { prefix: "result_" },
        processWithConfig: function(str) {
          const stringAlgo = new StringAlgorithms()
          const trimmed = stringAlgo.trim(str)
          return this.config.prefix + trimmed
        }
      }
      return stringOpConfigObj.processWithConfig("  hello  ") === "result_hello"
    })()
  )

  console.log(
    "11. 字符串操作一致性测试:",
    (() => {
      const testString = "  hello world  "
      const trimmed = stringAlgo.trim(testString)
      const upper = stringAlgo.changeCase(trimmed, true)
      const lower = stringAlgo.changeCase(trimmed, false)
      const length = stringAlgo.getStringLength(trimmed)
      
      return upper === "HELLO WORLD" &&
             lower === "hello world" &&
             length === 11
    })()
  )

  console.log(
    "12. 字符串操作边界测试:",
    (() => {
      const emptyResult = stringAlgo.trim("") === "" &&
                         stringAlgo.changeCase("", true) === "" &&
                         stringAlgo.getStringLength("") === 0 &&
                         stringAlgo.findSubstring("", "a") === -1
      
      const singleCharResult = stringAlgo.trim("a") === "a" &&
                              stringAlgo.changeCase("a", true) === "A" &&
                              stringAlgo.getStringLength("a") === 1 &&
                              stringAlgo.findSubstring("a", "a") === 0
      
      return emptyResult && singleCharResult
    })()
  )

  console.log(
    "13. 字符串操作性能测试:",
    (() => {
      const longString = "a".repeat(1000) + " " + "b".repeat(1000)
      const start = performance.now()
      stringAlgo.trim(longString)
      stringAlgo.changeCase(longString, true)
      stringAlgo.getStringLength(longString)
      stringAlgo.findSubstring(longString, "b")
      const end = performance.now()
      return (end - start) < 1000 // 性能测试应该在1秒内完成
    })()
  )

  console.log(
    "14. 字符串操作复杂测试:",
    (() => {
      const complexString = "  Hello World! 123  "
      const trimmed = stringAlgo.trim(complexString)
      const upper = stringAlgo.changeCase(trimmed, true)
      const lower = stringAlgo.changeCase(trimmed, false)
      const split = stringAlgo.splitString(trimmed, " ")
      const joined = stringAlgo.joinString(split, "-")
      
      return trimmed === "Hello World! 123" &&
             upper === "HELLO WORLD! 123" &&
             lower === "hello world! 123" &&
             JSON.stringify(split) === JSON.stringify(["Hello", "World!", "123"]) &&
             joined === "Hello-World!-123"
    })()
  )
}

testStringOperations()
